<template>
  <div class="tinymce-editor">
    <editor
      v-model="myValue"
      :init="init"
      :disabled="disabled"
      @onClick="onClick"
    >
    </editor>
  </div>
</template>
<script>
import tinymce from "tinymce/tinymce";
import Editor from "@tinymce/tinymce-vue";
import "tinymce/themes/silver";
import "tinymce/icons/default/icons"; //解决了icons.js 报错Unexpected token '<'
// 编辑器插件plugins
// 更多插件参考：https://www.tiny.cloud/docs/plugins/
import "tinymce/plugins/image"; // 插入上传图片插件
import "tinymce/plugins/lists"; // 列表插件
import "tinymce/plugins/wordcount"; // 字数统计插件
import "tinymce/plugins/quickbars";
import "tinymce/plugins/link";
import { readImg, compressImg } from "../../utils/utils";
import baseurl from "../../requestsetting/services/baseurl"

export default {
  components: {
    Editor,
  },
  props: {
    value: {
      type: String,
      default: "",
    },

    // 基本路径，默认为空根目录，如果你的项目发布后的地址为目录形式，
    // 即abc.com/tinymce，baseUrl需要配置成tinymce，不然发布后资源会找不到
    baseUrl: {
      type: String,
      default: "",
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    plugins: {
      type: [String, Array],
      default: "lists image wordcount quickbars link ",
    },
  },
  model: {
    prop: "value",
    event: "editorvaluechange",
  },
  data() {
    return {
      init: {
        language_url: `${this.baseUrl}/tinymce/langs/zh_CN.js`,
        language: "zh_CN",
        skin_url: `${this.baseUrl}/tinymce/skins/ui/oxide`,
        content_css: `${this.baseUrl}/tinymce/mycss/mycontent.css`,
        // skin_url: `${this.baseUrl}/tinymce/skins/ui/oxide-dark`, // 暗色系
        // content_css: `${this.baseUrl}/tinymce/skins/content/dark/content.css`, // 暗色系
        plugins: this.plugins,
        branding: false,
        resize: false,
        statusbar: false,
        toolbar: false, //隐藏工具栏
        menubar: false, //隐藏菜单栏
        inline: true,
        fontsize_formats: "16px 18px 20px 22px 24px 26px 36px 48px",
        placeholder: "在此区域开始编辑",
        object_resizing: false,
        quickbars_image_toolbar: false,
        // 此处为图片上传处理函数，这个直接用了base64的图片形式上传图片，
        // 如需ajax上传可参考https://www.tiny.cloud/docs/configure/file-image-upload/#images_upload_handler
        // eslint-disable-next-line no-unused-vars
        // images_upload_handler: (blobInfo, success, failure) => {
        //   console.log('imageuphad')
        //   const img = 'data:image/jpeg;base64,' + blobInfo.base64()
        //   success(img)
        // }
        // eslint-disable-next-line no-unused-vars
        images_upload_handler: (blobInfo, success, failure) => {
          this.handlePic(blobInfo, success);
        },
      },
      myValue: this.value,
    };
  },
  mounted() {
    tinymce.init({});
  },
  methods: {
    // 添加相关的事件，可用的事件参照文档=> https://github.com/tinymce/tinymce-vue => All available events
    // 需要什么事件可以自己增加
    onClick(e) {
      this.$emit("onClick", e, tinymce);
    },
    // 可以添加一些自己的自定义事件，如清空内容
    clear() {
      this.myValue = "";
    },
    // eslint-disable-next-line no-unused-vars
    async handlePic(blobInfo, success) {
      const that = this;
      console.log("处理图片");
      console.log(blobInfo);
      if (blobInfo != undefined) {
        let file = blobInfo.blob();
        const img = await readImg(file);
        const blob = await compressImg(img, img.type, 1000, 1000);
        let param = new FormData();
        param.append("file", blob, "file.jpg");
        console.log(param.get("file"));
        let result = that.uploadPicture(param, success);
        return result;
      }
    },
    uploadPicture(param, success) {
      let config = {
        headers: {
          "Content-Type": "multipart/form-data",
        },
        transformRequest: [
          function (data) {
            return data;
          },
        ],
      };
      if (param) {
        console.log("uploadpic");
        this.$getApi.uploadPostPicture(param, config).then((res) => {
          console.log(res);
          let result =
            baseurl + res.data.file;
          console.log(result);
          success(result);
          return result;
        });
      }
    },
  },
  watch: {
    value(newValue) {
      console.log("valuechange", newValue);
    },
    myValue(newValue) {
      this.$emit("editorvaluechange", newValue);
    },
  },
};
</script>

